home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Atari Compendium
/
The Atari Compendium (Toad Computers) (1994).iso
/
files
/
umich
/
tex
/
lgrind.zoo
/
lgrind.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-28
|
41KB
|
1,464 lines
/* lgrind.c, a %%\LaTeX%% version of tgrind, which is a %%\TeX%% version
* of vgrind. Used to `grind nice program listings'.
*/
#ifndef lint
static char sccsid[] = "@(#)@(#)tfontedpr.c 1.3 (LBL) 4/12/85";
static char Version[] =
"$Id: lgrind.c,v 1.4 91/10/01 00:36:11 gvr Exp $";
#endif
/* lgrind --- general purpose "pretty printer" for use with %%\LaTeX%%.
*
* Copyright %%\copyright%% 1985 by Van Jacobson, Lawrence Berkeley Laboratory
* This program may be freely used and copied but may not be sold
* without the author's written permission. This notice must remain
* in any copy or derivative.
*
* This program is an adaptation of "vfontedpr" v4.2 (12/11/84) from
* the 4.2bsd Unix distribution. Vfontedpr was written by Dave
* Presotto (based on an earlier program of the same name written by
* Bill Joy).
*
* I would welcome comments, enhancements, bug fixes, etc. Please
* mail them to:
* van@@lbl-rtsg.arpa (from arpanet, milnet, csnet, etc.)
* ..!ucbvax!lbl-csam!van (from Usenet/UUCP)
*
* Modifications.
* --------------
* 30 Mar 85 Chris & Van Fixed %|\C|% & %|\S|% (comment & string start
* indicators to really appear at the start of
* comments & strings. Changes for speeded-up
* @expmatch()@.
* 29 Mar 85 Chris Torek (chris@@maryland): Bug fixes for %|~|% and
* %|^L|% output. Most cpu-time eaters recoded
* to improve efficiency.
* 10 Feb 85 Van Written.
* 8 Oct 87 JSL Modified so as to compile on VMS. On VMS, we
* don't use a surrounding shell script, so the
* options are a bit different --- added %|-i|% to
* make a file to be %|\input|% in a %%\LaTeX%%
* environment. Changed some of the characters
* around --- e.g., we always take %|*|% in math
* mode (to make it centered) so we have to
* write %|*|% as %|\*|%. Etc.
* 14 Jan 88 JSL %|-e|% option for embedded code.
* 22 Jan 88 JSL Bugfix --- program could overrun @pstack@
* because of an incorrect limit test.
* Sep 91 George V Reilly Reformated and cleaned up code, including
* naughtiness with @NULL@. Added %|@|%,
* %|%%|%, %|%$|%, and %%\tt \%|%% features.
* Also the %|%<|%, %|%!|%, and %|%#|% features.
*/
#include <stdio.h>
#include <ctype.h>
#ifndef vms
# include <sys/types.h>
# include <sys/stat.h>
# include <time.h>
#else
# include <types.h>
# include <stat.h>
#endif
typedef int boolean;
#define TRUE 1
#define FALSE 0
#define STANDARD 0
#define ALTERNATE 1
#define NOTCODE 0 /* the three states of @incode@ */
#define INITCODE 1
#define OTHERCODE 2
#define PNAMELEN 80 /* length of a function/procedure name */
#define PSMAX 20 /* size of procedure name stacking */
#define BUFFERSIZE 4096
#ifndef vms
# define OKEXIT 0
# define BADEXIT 1
# ifndef DEFSFILE
# define DEFSFILE "/usr/lib/lgrindefs"
# endif
#else
# define OKEXIT 1
# define BADEXIT 0
# define DEFSFILE "TEX$INPUTS:lgrindefs.src"
#endif
/* regular expression routines */
char *expmatch(); /* match a string to an expression */
char *convexp(); /* convert expression to internal form */
/* database query routines */
char *tgetstr();
int tgetent();
int tgetflag();
/* forward declarations */
void setlang();
void readfile();
void putScp();
void putKcp();
void putVcp();
void putstr();
boolean isproc();
void outchar();
/*
* The state variables
*/
boolean incomm; /* in a comment of the primary type */
boolean instr; /* in a string constant */
boolean inchr; /* in a character constant */
int incode; /* in program text within a comment */
int latexcode; /* in program text within %%\LaTeX%% */
int latex_tt; /* in %|\tt|% text within %%\LaTeX%% */
boolean use_tt; /* use %|\tt|% everywhere */
boolean do_at; /* pay attention to %|@|%s in %%\LaTeX%% */
boolean do_tt; /* pay attention to %|||%s in %%\LaTeX%% */
boolean nokeyw = FALSE; /* no keywords being flagged */
boolean prccont; /* continue last procedure */
boolean code_cmnts = TRUE; /* Treat %|@|%, etc specially in comments */
boolean code_latex = TRUE; /* Treat %|@|%, etc specially in %%\LaTeX%% */
boolean linehead; /* at beginning of line or after whitespace */
int lastout; /* (extended) last character to outchar */
int comtype; /* type of comment */
int psptr; /* the stack index of the current procedure */
char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
int plstack[PSMAX]; /* the procedure nesting level stack */
int blklevel; /* current nesting level */
char *fname = ""; /* File being read */
int lineno; /* Line number in that file */
char pname[BUFFERSIZE+1]; /* Current procedure name */
/*
* The language specific globals
*/
char *defsfile = DEFSFILE; /* name of language definitions file */
char language[PNAMELEN]="c"; /* the language indicator */
char the_buf[BUFFERSIZE+1]; /* general purpose buffer */
char *buf = the_buf + 1; /* @buf-1@ must be valid */
char strings[2*BUFFERSIZE]; /* store the keywords */
char defs[2 * BUFFERSIZE]; /* language definitions from lgrindefs */
char *l_keywds[BUFFERSIZE/2];/* keyword table address */
char *l_prcbeg; /* regular expr for procedure begin */
char *l_combeg; /* regexp introducing a comment */
char *l_comend; /* regexp ending a comment */
char *l_acmbeg; /* regexp introducing a comment */
char *l_acmend; /* regexp ending a comment */
char *l_blkbeg; /* regexp beginning of a block */
char *l_blkend; /* regexp ending a block */
char *l_strbeg; /* regexp starting string constant */
char *l_strend; /* regexp ending string constant */
char *l_chrbeg; /* regexp starting character constant */
char *l_chrend; /* regexp ending character constant */
char *l_cdebeg; /* regexp starting prog text within comment */
char s_cdebeg[PNAMELEN+1]; /* actual string corresponding to @l_cdebeg@ */
char *l_cdeend; /* regexp ending prog text within comment */
char *l_texbeg; /* regexp starting %%\TeX%% text in comment */
char *l_texend; /* regexp ending %%\TeX%% text in comment */
char *l_txmbeg; /* regexp starting %%\TeX%% math in comment */
char *l_txmend; /* regexp ending %%\TeX%% math in comment */
char *l_tt_beg; /* regexp starting verbatim text in comment */
char *l_tt_end; /* regexp ending typewriter text in comment */
char *l_at; /* regexp for %|@|% in %%\LaTeX%% text */
char *l_tt; /* regexp for %|||% in %%\LaTeX%% text */
char *l_pc; /* regexp for %|%|% in %%\LaTeX%% text */
char l_escape; /* character used to escape characters */
boolean l_toplex; /* procedures only defined at top lex level */
boolean l_onecase; /* upper & lower case equivalent */
boolean embed = FALSE; /* -e seen --- do embedded code */
boolean code = TRUE; /* Looking at code */
/*
* global variables also used by expmatch
*/
extern boolean _escaped; /* if last character was an escape */
extern char *_start; /* start of the current string */
extern int (*re_strncmp)(); /* function to do string compares */
extern int strncmp();
extern int lc_strncmp();
int
main(argc, argv)
int argc;
char *argv[];
{
struct stat stbuf;
char *p;
boolean hseen = FALSE; /* -h seen */
char *hstring; /* header string to use */
boolean iseen = FALSE; /* -i seen */
int files_done = 0;
argc = getredirection(argc, argv);
argc--, argv++;
do {
register char *cp;
register int i;
if (argc > 0) {
if (!strcmp(argv[0], "-h")) {
hseen = TRUE;
if (argc == 1) {
hstring = "";
argc = 0;
goto rest;
}
hstring = argv[1];
argc--, argv++;
argc--, argv++;
if (argc > 0)
continue;
goto rest;
}
/* take input from the standard place */
if (!strcmp(argv[0], "-")) {
argc = 0;
goto rest;
}
/* Process embedded text */
if (!strcmp(argv[0], "-e")) {
embed = TRUE;
argc--, argv++;
continue;
}
/* format for inclusion */
if (!strcmp(argv[0], "-i")) {
iseen = TRUE;
argc--, argv++;
continue;
}
/* indicate no keywords */
if (!strcmp(argv[0], "-n")) {
nokeyw++;
argc--, argv++;
continue;
}
/* Don't treat %|@|%, etc. specially in comments */
if (!st